package com.bp.hearthealth.pulse.lnapps.ads.topon.banner_ad

import android.app.Activity
import android.view.View
import com.anythink.banner.api.ATBannerView
import com.bp.hearthealth.pulse.lnapps.ads.model.AdsSwitch
import com.bp.hearthealth.pulse.lnapps.ads.topon.ATAdSourceStatusListenerImpl
import com.bp.hearthealth.pulse.lnapps.bp_utils.Constants


class TopOnBannerAdManager {

    private var mBannerView: ATBannerView? = null
    private var sourceStatusListenerImpl: ATAdSourceStatusListenerImpl? = null
    private var atBannerExListenerImpl: ATBannerExListenerImpl? = null

    fun initBannerView(activity: Activity) {
        if (!AdsSwitch.getBoolean(AdsSwitch.adBannerControl)) {
            return
        }
        mBannerView = ATBannerView(activity)
        mBannerView!!.setPlacementId(Constants.TOP_ON_BANNER_ID)
        //加载和展示广告应保持容器和 BannerView 始终可见
        mBannerView!!.visibility = View.VISIBLE
        atBannerExListenerImpl = ATBannerExListenerImpl()

        mBannerView!!.setBannerAdListener(atBannerExListenerImpl)
        if (sourceStatusListenerImpl == null) {
            sourceStatusListenerImpl = ATAdSourceStatusListenerImpl()
        }
        mBannerView!!.loadAd()
//        mBannerView!!.setAdSourceStatusListener(sourceStatusListenerImpl)
    }

    fun setBannerListener(topOnBannerAdListener: TopOnBannerAdListener?) {
        atBannerExListenerImpl?.setBannerLoadListener(topOnBannerAdListener)
    }

    fun getBannerView(): ATBannerView? {
        if (!AdsSwitch.getBoolean(AdsSwitch.adBannerControl)) {
            return null
        }
        return mBannerView
    }

    fun releaseBannerAd(vararg destroy: Boolean) {
        if (mBannerView != null) {
            sourceStatusListenerImpl = null
            atBannerExListenerImpl = null

            mBannerView!!.setAdSourceStatusListener(null)
            mBannerView!!.setBannerAdListener(null)
            mBannerView!!.destroy()
        }
        if (destroy.isNotEmpty()) {
            mBannerView = null
        }
    }
}

